var layer = layui.layer //弹层
    ,table = layui.table //表格
form = layui.form,$ = layui.jquery,changeList=[],laydate = layui.laydate;

lay('.time').each(function(){
    laydate.render({
        elem: this
    });
});
layer.ready(function () {
    //获取所有用户信息
    $.ajax({
        url:"otherOutStock/getAllUserInfo",
        success:function(data){
            if(data.status == 200){
                for(let i in data.data){
                    $("#salesmanCode").append("<option value=\"" + data.data[i].code + "\">" + data.data[i].name + "</option>");
                }
                form.render('select');
            }
        }
    });
});
let productInfo = [];
table.render({
    elem: '#shipmentplan'
    , url: 'planInStore/add/getCarryDetailInfoByPage'
    ,cols: [[
        {checkbox: true, sort: true,title:'选择'}
        ,{field:'carryInvoiceNo', title:'出运单号',sort:true}
        ,{field:'companyName', title:'签约单位',sort:true}
        ,{field:'destination', title:'目的地',sort:true}
        ,{field:'carryDate', title:'出运日期',sort:true}
        ,{field:'createDate', title:'创建日期',sort:true}
    ]]
    ,id: 'shipmentplantableid'
    ,height: 300
    ,page: true
    , parseData: function(res){
        return {
            "code": res.status,
            "count": res.total,
            "data": res.data
        };
    }
    ,done: function (res, curr, count) {
        table.render({
            elem: '#product'
            ,data:productInfo
            ,url:"planInStore/add/getCarryDetailChildInfoByPage"
            ,cols: [[
                {checkbox: true, sort: true,title:'选择'}
                ,{field:'productCode', title:'产品编码', sort: true}
                ,{field:'specificationCode', title:'规格编码', sort: true}
                ,{field:'specificationExplanation', title:'规格说明', sort: true}
                ,{field:'productCname', title:'中文名称', sort: true}
                ,{field:'productEname', title:'英文名称', sort: true}
                ,{field:'productNum', title:'产品数量', sort: true}
                ,{field:'referenceQuantity', title:'剩余数量', sort: true}
                ,{field:'productUnit', title:'单位', sort: true}
                ,{field:'netWeight',title: '净重',sort: true}
                ,{field:'grossWeight',title: '毛重',sort: true}
                ,{field:'bulk', title:'体积', sort: true}
                ,{field:'purchaseNo', title:'采购单号', sort: true}
            ]]
            ,id: 'producttableid'
            ,where: {
                id:res.data.length == 0 ? "":res.data[0].carryDetailId //默认第一条信息，数据为空则id为空
            }
            ,height: 400
            ,done:function () {
                $("th .layui-table-cell.laytable-cell-checkbox").html("选择");
            }
        });
    }
});
//监听复选框主表
table.on('checkbox(shipmentplantable)', function (data) {
    if(data.type == 'all'){
        //allDataOperation(data);
    }else if(data.type == 'one'){
        oneDataOperation(data);
    }
});
//复选框主表单选
function oneDataOperation(data) {
    let id = data.data.carryDetailId;
    table.reload('producttableid', {
        page: {
            limit: 10,
            curr: 1 //重新从第 1 页开始
        }
        ,where: {
            id:id
        },
        done: function (res, curr, count) {
            var child = $("#product").next().find('tbody input[type="checkbox"]');
            child.each(function(index, item){
                item.checked = data.checked;
            });
            if(data.checked){
                for(var i = 0; i < res.data.length; i++){
                    changeList.push(res.data[i]);
                }
            }else{
                if(changeList.length > 0){
                    for(var i = 0; i < res.data.length; i++){
                        deleteData(res.data[i]);
                    }
                }
            }
            form.render('checkbox');

            $("th .layui-table-cell.laytable-cell-checkbox").html("选择");
        }
    }, 'data');
}
//删除元素
function deleteData(data){
    let id = data.carryDetailId;
    let stockId = data.carryDetailProductId;
    for(var j = 0; j < changeList.length; j++){
        if (changeList[j].carryDetailId == id && changeList[j].carryDetailProductId == stockId) {
            changeList.splice(j, 1); // 将使后面的元素依次前移，数组长度减1
            j--; // 如果不减，将漏掉一个元素
        }
    }
}

//监听复选框子表
table.on('checkbox(producttable)', function (obj) {
    if ( obj.type == 'one' ) {
        if (obj.checked) {
            changeList.push(obj.data);
        } else {
            for (let index in changeList) {
                if (changeList[index].carryDetailProductId == obj.data.carryDetailProductId && changeList[index].carryDetailId == obj.data.carryDetailId) {
                    changeList.splice(index,1);
                }
            }
        }
    } else if (obj.type == 'all') {
        // 子表数全选待处理

    }
    form.render('checkbox');
});
//主表单击行事件
table.on('row(shipmentplantable)', function (obj) {
    let id = obj.data.carryDetailId;
    table.reload('producttableid', {
        page: {
            limit: 10,
            curr: 1 //重新从第 1 页开始
        }
        ,where: {
            id:id
        },
        parseData: function (res) {
            if (changeList) {
                for (let i in res.data) {
                    for (let j in changeList) {
                        if (changeList[j].carryDetailProductId == res.data[i].carryDetailProductId &&  changeList[j].carryDetailId == res.data[i].carryDetailId) {
                            res.data[i].LAY_CHECKED = 'true';
                        }
                    }
                }
            }
            return res;
        },
        done: function (res, curr, count) {
            $("th .layui-table-cell.laytable-cell-checkbox").html("选择");
        }
    }, 'data');
});

//提交查询条件
form.on('submit(submitform)', function(data){
    table.reload('shipmentplantableid', {
        page: {
            limit:10,
            curr: 1 //重新从第 1 页开始
        }
        ,where: data.field
    }, 'data');
    return false;
});

//用户选择数据
$("#choose").on("click",function () {
    if(changeList.length == 0){
        layer.msg("请选择数据")
    }else{
        let oldData =  parent.table.cache["storageproductstableid"];
        $.each(changeList, function (index, value) {
            //剔除重复数据
            let flag = true;
            if(oldData.length > 0){
                for(var i = 0; i < oldData.length; i++){
                    let sameFlag = oldData[i].carryDetailProductId == changeList[index].carryDetailProductId && oldData[i].carryDetailId == changeList[index].carryDetailId;
                    if(sameFlag){
                        flag = false;
                        break;
                    }
                }
            }
            if(flag){
                //不存在重复数据,添加数据
                oldData.push(changeList[index]);
            }
        });
        parent.table.reload('storageproductstableid', {data:oldData});
        setTimeout(function () {
            //保存成功则关闭此页面
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        },)
    }
})
$("#shutdown").on("click",function () {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
})